#include "sensor.h"

#define INTERVAL 30

// Prints out a given error.
void error(const char *msg) { perror(msg); }

// Client process. Sends request to repository.
void *client(void *t_id) {
  struct TDATA *tdata;
  int id = (int) t_id;
  int sid=0, dest;
  int i1,t1,r1,s1,i2,t2,r2,s2;
  char req[BSIZE];

  tdata->src  = id; 
  tdata->type = PULL; 
  while (true) {
    srand(time);
    usleep(rand()%INTERVAL);
     i1 = rand()%NUM_SENSORS;
     i2 = rand()%NUM_SENSORS;
     t1 = time(NULL) - rand()%INTERVAL; 
     t2 = time(NULL) - rand()%INTERVAL;
     s1 = SPEED_MIN + rand()%SPEED_RANGE;
     s2 = SPEED_MIN + rand()%SPEED_RANGE;
     r1 = rand()%NUM_ROADS;
     r2 = rand()%NUM_ROADS;
     dest   = PORT+RBEG+(sid)%NUM_REPOS;  
    bzero(req, BSIZE);
    sprintf(req, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n",
                 id,dest,DATA,i1,t1,r1,s1,i2,t2,t2,s2);
    tdata->dest = dest; 
    strcpy(tdata->msg, req); 
    // TODO: Create thread for sender.
     sender(tdata);
  }
  return;
}

// Create client threads; then wait until they all finish.
int main () {
  int i;
  pthread_t thread[NUM_TOTAL];
  for (i=CBEG; i<CEND; i++) pthread_create((pthread_t *) thread[i], NULL, client, (void *) i); 
  for (i=CBEG; i<CEND; i++) pthread_join((pthread_t *) thread[i], NULL);
  return 0;
}
